Ext.define('PJ.view.messagemanagement.ReceiveMessagesList', {
    extend: 'AM.default.DefaultList',
    requires: ['AM.default.DefaultSearchForm', 
               'AM.default.DateTime', 
               'AM.default.DefaultDateField'
               ],
    initComponent: function(){
    	var me = this;
        me.columns = [{
    		header: '通知标题',
    		dataIndex: 'newsTitle',
    		flex: 1,
            minWidth: 120,
    		renderer: function(value, meta, record){
    			meta.tdAttr = "newsId='" + record.get("newsId") + "'newsTypeCode='"+record.get("newsTypeCode")+"'className='"+record.get("className")+"'";
    			if (record.get("readState") == 1)
    				meta.style = "white-space:normal;cursor: pointer;color:blue;";
    			if (record.get("readState") == 2)
    				meta.style = "white-space:normal;cursor: pointer;color:red;";
    			return value;
    		},
    		listeners: {
    			click: function(g, clm){
    				var newsId = clm.getAttribute("newsId");
    				var win = Ext.create("PJ.view.messagemanagement.View", {
            			title: "查看通知",
            			text:"接收通知",
            			grid:me
            		}).show();
    				var actionName = "load";
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					actionName = "loadJsonp";
    				}
    				win.down("form").getForm().doAction(actionName,{
    					url:me.store.preUpdateUrl,
    					params:{
    						newsId: newsId
    					},
    					success:function(f,action){
    						var data = action.response.responseText;
    	        			var json = gj_onlineLearning_constant.loadType=="ajax" ? Ext.decode(data, true) : action.response;
    	        			var form = win.down('form');	 
    	        			var baseForm = form.getForm();
    	        			if(json.data.newsTypeCode=="xxlx_pxb"){
    	        				baseForm.findField("specialName").setVisible(true);
    	        				baseForm.findField("specialName").setValue(json.data.className);
    	        				win.setHeight(360);
    	        			}else{
    	        				baseForm.findField("specialName").setVisible(false);
    	        				win.setHeight(320);
    	        			}
    	        			//实时
//    	        			Ext.Ajax.request({
//    	        				url: showMsgUrl,
////    	              		url: 'workbench/orchid/data/msgData.jsons',
//    	        				params: {},
//    	        				success: function(response){
//    	        					var data = response.responseText;
//    	        					var jsonData = Ext.decode(data, true);
//    	        					var remindUserNoReadCount = jsonData.data.remindUserNoReadCount;
//    	        					var newsUserNoReadCount = jsonData.data.newsUserNoReadCount;
//    	        					//me.setTitle("提醒管理("+remindUserNoReadCount+")")
//    	        					Ext.get("li_"+"8a10e7f9557b199401557b37dfbf0001").dom.innerHTML = '<span class="title"><b class="bull"></b>通知管理('+newsUserNoReadCount+')</span>'	
//    	        					/*Ext.get("li_"+"4028809c52207e8a015220860c5r0002").dom.innerHTML = '<span class="title"><b class="bull"></b>提醒管理('+remindUserNoReadCount+')</span>'*/	
//    	        				}
//    	        			});
    	        			me.store.load();
//    	        			baseForm.findField("newsType").setValue(json.data.newsType.dataName);
    	        			//取到值后直接传给model
    	                	var model = Ext.create("PJ.model.messagemanagement.AttachmentList");
    	                    model.set({
    	                        "attachmentId": json.data.attachmentId,
    	                        "attachName": json.data.attachName,
    	                        "attachType":  json.data.attachType,
    	                        "fileSize": json.data.fileSize,
    	                    });
    	                    var record = new Array();
    	                    var store = form.down("attachmentList").getStore();
    	                    record.push(model);
    	                    store.add(record);
    					}
    				});

    			}
    		}
    	}, {
            header: '通知类型',
            dataIndex: 'newsType',
            width:120,
    		renderer: function(value, metaData, record){
    			metaData.tdAttr = 'data-qtip="' + value + '"';
    			return value;
    		}
        }, {
            header: '发送人',
            dataIndex: 'userName',
    		flex: .5,
            minWidth:150,
    		renderer: function(value, metaData, record){
    			metaData.tdAttr = 'data-qtip="' + value + '"';
    			return value;
    		}
        },{
            header: '发送时间',
            dataIndex: 'sendTime',
            width: 120 
        }, {
            header: '读取状态',
            dataIndex: 'readState',
            width:120,
    		renderer: function(value, metaData, record){
    			metaData.tdAttr = 'data-qtip="' + value + '"';
    			if(value==1){
    				return "<span style='color:blue'>已读</span>"
    			}else if(value==2){
    				return "<span style='color:red'>未读</span>"
    			}
    		}
        },/* {
            header: '是否回复',
            dataIndex: 'erplyState',
            width:120,
    		renderer: function(value, metaData, record){
    			metaData.tdAttr = 'data-qtip="' + value + '"';
    			if(value==1){
    				return "是"
    			}else if(value==2){
    				return "否"
    			}
    		}
        },*/{
            header: '回复',
            menuDisabled: true,
            sortable: false,
            resizable: false,
            hideable: false,
            xtype: 'actioncolumn',
            width: 60,
            items: [{
            	tooltip: '回复',
            	getClass: function(v, meta, rec){
            		meta.style = "cursor: pointer;";
            		return 'templateIco';
        		},
                handler: function(grid, rowIndex, colIndex, item, eve, record){
                	var newsId = record.get("newsId");
                	var newsUserLink = record.get("newsUserLink");
    				var win = Ext.create("PJ.view.messagemanagement.ReplyEdit", {
            			title: "回复内容",
            			text:"接收通知",
            			grid:grid,
            			newsId:newsId,
            			newsUserLink:newsUserLink
            		}).show();
    				var actionName = "load";
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					actionName = "loadJsonp";
    				}
    				win.down("form").getForm().doAction(actionName,{
    					url:me.store.preUpdateUrl,
    					params:{
    						newsId: newsId
    					},
    					success:function(f,action){
//    						Ext.Ajax.request({
//    	                		url: showMsgUrl,
////    	              		url: 'workbench/orchid/data/msgData.jsons',
//    	                		params: {},
//    	                		success: function(response){
//    	                			var data = response.responseText;
//    	                			var jsonData = Ext.decode(data, true);
//    	                			var remindUserNoReadCount = jsonData.data.remindUserNoReadCount;
//    	                			var newsUserNoReadCount = jsonData.data.newsUserNoReadCount;
//    	                			//me.setTitle("提醒管理("+remindUserNoReadCount+")")
//    	                			 Ext.get("li_"+"8a10e7f9557b199401557b37dfbf0001").dom.innerHTML = '<span class="title"><b class="bull"></b>通知管理('+newsUserNoReadCount+')</span>'	
//									 /*Ext.get("li_"+"4028809c52207e8a015220860c5r0002").dom.innerHTML = '<span class="title"><b class="bull"></b>提醒管理('+remindUserNoReadCount+')</span>'*/	
//    	                		}
//    	                	});
    						var data = action.response.responseText;
    	        			var json = gj_onlineLearning_constant.loadType=="ajax" ? Ext.decode(data, true) : action.response;
//    	        			var newsUserLink = json.data.newsUserLink;
    	        			var form = win.down('form');	 
    	        			var baseForm = form.getForm();
    	        			if(json.data.newsTypeCode=="xxlx_pxb"){
    	        				baseForm.findField("specialName").setVisible(true);
    	        				baseForm.findField("specialName").setValue(json.data.className);
    	        				win.setHeight(570);
    	        			}else{
    	        				baseForm.findField("specialName").setVisible(false);
    	        				win.setHeight(540);
    	        			}
    	        			var list = win.down("restoredMessageList");
    	        			list.newsUserLink = newsUserLink;
    	        			var store = list.store;
    	                    var params = store.proxy.extraParams;
    	                    var newParams = {
    	                    		searchNewsId: newsId,
    	                    		searchNewsUserLink: newsUserLink
    	                    };
    	                    Ext.apply(params, newParams);
    	                    store.isLoad = true;
    	                    store.load();
    					}
    				});
                }
            },{
            	getClass: function(v, meta, rec){
            		if(rec.get("isNewReply")==1){
            			return 'newSignIco';
            		}
        		},
            }]
        }];
        me.store = Ext.create('PJ.'+ prototype +'.messagemanagement.ReceiveMessagesList', {
            isLoad: false
        });
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        me.callParent(arguments);
    },
	//删除
	onDeleteClick:function(){
  	  var me = this;
  	  var store = me.getStore();
  	  var records = me.getSelectionModel().getSelection();
  	  for(var i=0;i<records.length;i++){
  		  if(records[i].get("t.publishState")==1){
  			Ext.Msg.show({
  				  title: '提示',
  			  	  msg  : '不允许删除已发布的数据！',
  			  	  icon :Ext.Msg.WARNING,
  			  	  buttons:Ext.Msg.OK
  			  });
  			  return;
  		  }
  	  };
  	Ext.Msg.confirm({
		  title:'提示',
		  msg:'您确定要删除这些数据吗?',
		  icon:Ext.Msg.WARNING,
		  buttons:Ext.Msg.OKCANCEL,
		  fn:function(id){
			  if(id=='ok'){
				  var newsId= new Array();
				  var newsUserLink= new Array();
				  Ext.Array.each(records,function(name,index){
//					  newsId.push(name.get('newsId'));
					  newsId.push(name.get('newsUserLink'));
				  });
				  if(gj_onlineLearning_constant.loadType == "jsonp"){
					  var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                  		loadType: gj_onlineLearning_constant.loadType,
                          url: store.deleteUrl,
                  		  extraParams: {
                  			newsIds: newsId,
                  			lookState: 2
                          }
                  	});
                  	store1.load(function(rec, operation, success){
                  		if(success){
                              Ext.Msg.alert('通知', '删除成功！', function(){
//                            	  Ext.Ajax.request({
//									  url: showMsgUrl,
////									  url: 'workbench/orchid/data/msgData.jsons',
//									  params: {},
//									  success: function(response){
//										  var data = response.responseText;
//										  var jsonData = Ext.decode(data, true);
//										  var remindUserNoReadCount = jsonData.data.remindUserNoReadCount;
//										  var newsUserNoReadCount = jsonData.data.newsUserNoReadCount;
//										  //me.setTitle("提醒管理("+remindUserNoReadCount+")")
//										  Ext.get("li_"+"8a10e7f9557b199401557b37dfbf0001").dom.innerHTML = '<span class="title"><b class="bull"></b>通知管理('+newsUserNoReadCount+')</span>'	
//										  /*Ext.get("li_"+"4028809c52207e8a015220860c5r0002").dom.innerHTML = '<span class="title"><b class="bull"></b>提醒管理('+remindUserNoReadCount+')</span>'*/	
//									  }
//								  });
                                  var store = me.getStore();
                                  store.load();
                              });
                          }else{
                              Ext.Msg.alert('通知', operation.error);
                          }
                  	});
				  }else{
					  Ext.Ajax.request({
						  url:store.deleteUrl,
						  params: {
							  newsIds: newsId,
							  lookState:2
	                       },
						  success:function(response){
							  var data=response.responseText;
							  var json=Ext.decode(data,true);
							  if(json.success){
								  Orchid.alert('提示','删除成功!',function(){
//									  Ext.Ajax.request({
//										  url: showMsgUrl,
////										  url: 'workbench/orchid/data/msgData.jsons',
//										  params: {},
//										  success: function(response){
//											  var data = response.responseText;
//											  var jsonData = Ext.decode(data, true);
//											  var remindUserNoReadCount = jsonData.data.remindUserNoReadCount;
//											  var newsUserNoReadCount = jsonData.data.newsUserNoReadCount;
//											  //me.setTitle("提醒管理("+remindUserNoReadCount+")")
//											  Ext.get("li_"+"8a10e7f9557b199401557b37dfbf0001").dom.innerHTML = '<span class="title"><b class="bull"></b>通知管理('+newsUserNoReadCount+')</span>'	
//											  /*Ext.get("li_"+"4028809c52207e8a015220860c5r0002").dom.innerHTML = '<span class="title"><b class="bull"></b>提醒管理('+remindUserNoReadCount+')</span>'*/	
//										  }
//									  });
									  store.load();
								  });
							  }else{
								  Orchid.alert('提示',json.msg);
							  }
						  }
					  });
				  }
			  }
		  }
	  });
	},
    listeners: {
    	selectionchange: function(selModel, selected){
            this.down('#' + this.oldId + "Delete").setDisabled(selected.length === 0);
        },
    	viewready: function(){
    		var me = this;
    		me.addDocked([{
    			dock: 'top',
    			xtype: "searchform",
    			columnCount: 3,
    			fieldDefaults: {
    				labelAlign: 'right',
    				labelWidth: 70
    			},
    			items: [{
    				xtype: "textfield",
    				fieldLabel: '通知标题',
    				name: "searchNewsTitle",
    				maxLength: "50",
    				emptyText: "请输入通知标题"
    			},{
    				xtype: "textfield",
    				fieldLabel: '发送人',
    				name: "searchUserName",
    				maxLength: "50",
    				emptyText: "请输入发送人"
    			},{
                    xtype: "combo",
                    fieldLabel: '通知类型',
                    name: "searchNewsCode",
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: gj_onlineLearning_constant.loadType == "jsonp" ? gj_onlineLearning_constant.loadType:'ajax',
                            url : me.store.messageTypeUrl+"?typeCode=xxlx",
                            xtype: 'combo', 
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    emptyText: "请选择"
                }, {
    				xtype: 'defaultDate',
    				fieldLabel: '发送时间',
    				name: 'searchBeginDate',
    				id: 'searchTrainingManagementRMSG',
    				vtype: 'startLessEndDate',
    				startLessEndDate: {
    					targetCmpId: 'searchTrainingManagementRMsg',
    					isStart: true
    				},
    				emptyText: "请输入开班时间"
    			}, {
    				xtype: 'defaultDate',
    				fieldLabel: '~',
    				labelSeparator: '',
    				labelWidth: 20,
    				name: 'searchEndDate',
    				id: 'searchTrainingManagementRMsg',
    				vtype: 'startLessEndDate',
    				vtypeText: '结束时间不能早于开始时间！',
    				startLessEndDate: {
    					targetCmpId: 'searchTrainingManagementRMSG',
    					isStart: false
    				},
    				emptyText: "请输入结业时间"
    			},{
					xtype: "combo",
					fieldLabel: '是否回复',
					name: "searchErplyState",
					store: Ext.create("Ext.data.ArrayStore", {
						fields: ['value', 'text'],
						data: [[1, '是'],[2,'否']]
					}),
					valueField: 'value',
					displayField: 'text',
					editable: false,
					emptyText: "请选择"
				},{
					xtype: "combo",
					fieldLabel: '读取状态',
					name: "searchReadState",
					store: Ext.create("Ext.data.ArrayStore", {
						fields: ['value', 'text'],
						data: [[1, '已读'],[2,'未读']]
					}),
					valueField: 'value',
					displayField: 'text',
					editable: false,
					emptyText: "请选择"
				}],
    			grid: this
    		}, {
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [{
					text: '删除',
					iconCls: 'deleteIco',
					itemId: this.oldId + "Delete",
					disabled: true,
					scope: this,
					handler: this.onDeleteClick
				},"->", this.countSelectPanel]
            }]);
            var store = this.store;
            var params = store.proxy.extraParams;
            var newParams = {};
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.load();
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    	}
    }
});
